<script setup lang="ts">
import { Handle, Position, useVueFlow } from '@vue-flow/core'
import { computed,ref, watch } from 'vue'
const props = defineProps<{
  id: string;
  data: {
    name: string;
    route?: string;
    enWS?: boolean;
    enInterrupt?: boolean;
  };
  style?: Record<string, any>; // 添加 style 属性
}>()

const { updateNodeData, } = useVueFlow()

const name = computed({
  get: () => props.data.name,
  set: (name) => updateNodeData(props.id, {...props.data, name: name }),
})


import useConnectionValid from './ts/connectionValid';
const { isValidConnection } = useConnectionValid();

// 节点属性
const textValue = ref(props.data.route);
const enWS = ref(props.data.enWS);
const enInterrupt = ref(props.data.enInterrupt);
watch(() => [textValue.value, enWS.value, enInterrupt.value], ()=>{
  updateNodeData(props.id, {...props.data,route: textValue.value, enWS: enWS.value, enInterrupt: enInterrupt.value })
})
defineOptions({
  name: 'HttpNode'
})  
</script>

<template>
    <div class="NodeName"> <input :id="`${id}-input`" v-model="name"  class="nodrag" /> </div>
    <div class="NodeContent">
      <Handle id="flow-end" type="target" :class="['AgentFlowInputHandle','top-0']"  :position="Position.Left"  title="Flow End"  :connectable="()=>{return isValidConnection(props.id,'flow-end', 'target')}"><span class="HandleLeftSpan">Flow End</span></Handle>
      <Handle id="return-msg"  type="target" :class="['AgentMsg','top-2']" :position="Position.Left"  title="Return Message"  :connectable="()=>{return isValidConnection(props.id,'return-msg', 'target')}"><span class="HandleLeftSpan">Return Message</span></Handle>
      
      <Handle id="flow-next"  type="source" :class="['AgentFlowOutputHandle','top-0']"  :position="Position.Right"  title="FLow Next" ><span class="HandleRightSpan" :connectable="()=>{return isValidConnection(props.id,'flow-next', 'source')}">Flow Next</span> </Handle>
      <Handle id="query-msg"  type="source" :class="['AgentMsg','top-2']" :position="Position.Right"  title="Query"  :connectable="()=>{return isValidConnection(props.id,'query-msg', 'source')}"><span class="HandleRightSpan">Query</span></Handle>
      <Handle id="interrupt-flow-next"  type="source" :class="['AgentFlowOutputHandle','top-3']" :position="Position.Right"  title="Interrupt Flow" :connectable="()=>{return isValidConnection(props.id,'interrupt-flow-next', 'source')}"><span class="HandleRightSpan">Interrupt Flow</span></Handle>
      <div class="NodeData">
        <!-- 第一行：textinput -->
        <div class="input-row">
          <label for="textInput">Route:</label>
          <input
            id="textInput"
            type="text"
            v-model="textValue"
            placeholder="/your-route"
            :class="['text-input','nodrag']"
          />
        </div>

        <!-- 第二行：checkbox 1 -->
        <div class="checkbox-row">
          <label for="enWS">Enable WebSocket</label>
          <input
            id="enWS"
            type="checkbox"
            v-model="enWS"
            :class="['checkbox','nodrag']"
          />
        </div>

        <!-- 第三行：checkbox 2 -->
        <div class="checkbox-row">
          <label for="enInterrupt">Enable Interrupt</label>
          <input
            id="enInterrupt"
            type="checkbox"
            v-model="enInterrupt"
            :class="['checkbox','nodrag']"
          />
        </div>
      </div>
    </div>

</template>


<style scoped>

.NodeName {
  position: relative;
  display: inline-block;
  font-size: 14px;
  width: 100%;
  padding-left: 70px;
  padding-right: 70px;
  text-align: center;
}

.NodeName input {
  max-height: 20px;
  height: 100%;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: #333;
  font-size: 16px;
  padding: 8px 12px;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
  text-align: center;
  background-color: #00000000;
}

/* 输入框聚焦时 */
.NodeName input:focus {
  background-color: #f5f5f5;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.5);
  color: #000;
}

/* 输入框没有内容时（placeholder显示） */
.NodeName input:placeholder-shown {
  color: #ffffff;
}

/* 输入框内容区域边距 */
.NodeName input::placeholder {
  color: #ccc;
  font-style: italic;
}

/* 输入框平滑过渡效果 */
.NodeName input {
  transition: background-color 0.3s, box-shadow 0.3s, color 0.3s;
}
.NodeContent{
  height: 100%;
  width: 100%;
}




</style>